package com.example.mobile2021606170hsy0417homework;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    public boolean isInMultiwindowMode() {
        return super.isInMultiWindowMode();

    }

    private TextView tvMainTitle;
    private Button btnMainText;
    private TextView tvMainSupplier;
    private TextView tvMainCommodity;
    private TextView tvMainEsult;
    private Button btnMainSubmit;
    private Button btnMainQuit;
    private String[] parts = new String[]{"轿车引擎", "轿车前玻璃", "轿车方向盘", "轿车方向盘", "轿车方向盘", "轿车方向盘"};
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择商品");
                builder.setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface , int i, boolean b) {
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tvMainEsult.setText(null);
                        for (int i = 0; i < parts.length-1; i++) {
                            if (listView.getCheckedItemPositions().get(i) && tvMainEsult.equals(null)) {
                                tvMainEsult.append(parts[i] + "\n");
                            }
                        }
                        dialog.dismiss();
                    }
                });
                dialog = builder.create();
                listView = dialog.getListView();
                dialog.show();

            }
        });

        btnMainQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("是否确定退出");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                })
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();

            }
        });
    }
        private void initView () {
            tvMainTitle = findViewById(R.id.tv_main_title);
            btnMainText = findViewById(R.id.btn_main_text);
            tvMainSupplier = findViewById(R.id.tv_main_supplier);
            tvMainCommodity = findViewById(R.id.tv_main_commodity);
            tvMainEsult = findViewById(R.id.tv_main_esult);
            btnMainSubmit = findViewById(R.id.btn_main_submit);
            btnMainQuit = findViewById(R.id.btn_main_quit);
        }
    }


